﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS;
using System.Collections;

namespace CMS
{
    public partial class ctrShopingCart : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AjaxPro.Utility.RegisterTypeForAjax(typeof(ctrShopingCart));

                lblHTML.Text = strCart();
            }
        }
        [AjaxPro.AjaxMethod]
        public string strCart()
        {
            string html =string.Empty;
            html += "<div id=\"divContentShop\">";
            html += "<li><div><h2>Thông tin giỏ hàng</h2>";
            html += "<table cellspacing=\"3\" cellpadding=\"5\" id=\"ShopMaster1_gvCart\" style=\"width: 100%;border-collapse: collapse;\">";
            html += "<tbody> <tr class=\"tr_shop_mas\">";
            html += "<th class=\"td_center\" scope=\"col\">";
            html += "SP</th><th class=\"td_center\" scope=\"col\">SL </th>";
            html += " <th class=\"td_last\" scope=\"col\">Thành tiền</th></tr>";
            html += UpdateShoppingCart();
            html += "<tr class=\"tr_s_no\"> <td class=\"td_center\">";
            html += " <input type=\"submit\" value=\"Xóa hết\" onclick=\"return confirm('Bạn có chắc chắn muốn xóa hết giỏ hàng không?');\"";
            html += "id=\"cphLeft_ShopStep1_btnClear\" class=\"btn_shop\" /></td><td class=\"td_quantity\">&nbsp;</td>";
            html += "<td class=\"td_last\">&nbsp;<input type=\"submit\" value=\"Thanh toán\" id=\"Submit2\" class=\"btn_shop\" onclick=\"window.location='/Default.aspx?Order=Complete';return false;\"/></td></tr></tbody></table></div></li>";
            html += "</div>";
            return html;
        }
        decimal GrandTotal = 0;

        private string UpdateShoppingCart()
        {
            string str = "";

            ShopItemCls[] ShopItems = ShopCart.GetShoppingCarts();

            if (ShopItems.Length > 0)
            {
                for (int iIndex = 0; iIndex < ShopItems.Length; iIndex++)
                {
                    str +=
                          "<tr class=\"tr_s_no\">" +
                          "<td class=\"td_center\">" + ShopItems[iIndex].ProductCode + "</td>" +
                          "<td class=\"td_quantity\">" + ShopItems[iIndex].Quantity + "</td>" +
                          "<td class=\"td_last\">" + ShopItems[iIndex].SalePriceVnd + "</td></tr>";

                    GrandTotal = GrandTotal + ShopItems[iIndex].SubTotal;
                }
                str += "<tr class=\"tr_s_no\"><td class=\"td_center\"><span style=\"font-weight: bold;\">Tổng tiền</span></td><td class=\"td_quantity\">&nbsp;</td>";
                str += "<td class=\"td_last\"><span style=\"color: rgb(255, 0, 0); font-family: Tahoma, ; font-size: 12px;font-style: normal; font-variant: normal; font-weight: bold; text-align: right; text-indent: 0px;  widows: 2; word-spacing: 0px; background-color: rgb(242, 242, 242); display: inline !important;float: none;\">" + GrandTotal.ToString("#,##") + "</span></td></tr>";
            }
            return str;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string ServerSideRemove(string ProductCode)
        {
            ArrayList ArrayListTemp = new ArrayList();
            ShopItemCls[] ShopItems = ShopCart.GetShoppingCarts();
            for (int iIndex = 0; iIndex < ShopItems.Length; iIndex++)
            {
                if (ShopItems[iIndex].ProductCode.Equals(ProductCode) == false)
                {
                    ArrayListTemp.Add(ShopItems[iIndex]);
                }
            }

            System.Web.HttpContext.Current.Session["ArrayListCarts"] = ArrayListTemp;
            return UpdateShoppingCart();
        }

    }
}